<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/10/31
 * Time: 10:45 AM
 */
namespace app\frontend\modules\site\controllers;

use app\common\models\Category;
use app\common\models\ThemeFile;
use app\common\services\theme\ThemeService;
use app\frontend\modules\site\services\ProductService;
use app\frontend\modules\site\services\SiteBaseService;

class ProductController extends SiteBaseController
{
    public function getList(){
        $cid = request()->cid;
        $name = request()->name;
        $themeService = new ThemeService();
        $themeInfo = $themeService->getDefaultThemeInfo($this->clientType,ThemeFile::TYPE_PRODUCT_LIST);

        $themeFileInfo = $themeInfo['themeFiles'][0];
        $pageData = $themeService->pageData($themeFileInfo);
        $datas = $pageData['datas'];
        $ProductService = new ProductService();
        $list = $ProductService->getList([
            'cid'=>$cid,
            'name'=>$name
        ]);
        $view = $themeFileInfo['file'];
        $siteInfo = (new SiteBaseService())->siteInfo();
        $categoryInfo = [];
        if($cid){
            $categoryInfo = Category::getById($cid);
            $siteInfo = $ProductService->fmtListSeoInfo($siteInfo,$categoryInfo);
            $view = $ProductService->getListView($this->clientType,$view,$categoryInfo);
        }
        $view = $themeInfo['theme'].'.'.$view;

        return view($view, [
            'site_info'=>$siteInfo,
            'category_info'=>$categoryInfo,
            'list'=>$list,
            'vars' =>$datas,
            'cid'=>$cid
        ]);
    }
    public function detail(){
        $id = request()->id;
        $themeService = new ThemeService();
        $themeInfo = $themeService->getDefaultThemeInfo($this->clientType,ThemeFile::TYPE_PRODUCT_DETAIL);
        $themeFileInfo = $themeInfo['themeFiles'][0];
        $pageData = $themeService->pageData($themeFileInfo);
        $datas = $pageData['datas'];
        $productService = new ProductService();
        $productDetail = $productService->getDetail($id);
        $productHot = $productService->getHot();
        $productInfo = $productDetail['info'];
        $siteInfo = (new SiteBaseService())->siteInfo();
        $siteInfo = $productService->fmtDetailSeoInfo($siteInfo,$productInfo);
        $view = $themeFileInfo['file'];
        $view = $productService->getDetailView($this->clientType,$view,$productInfo);
        $view = $themeInfo['theme'].'.'.$view;
        return view($view, [
            'site_info'=>$siteInfo,
            'product_info'=>$productInfo,
            'product_hot' =>$productHot,
            'vars' =>$datas
        ]);
    }

}
